package activity.store;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.hutoubenjg.R;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.ObjectAnimator;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import activity.login.ActivityLogin;
import base.BaseActivity;
import base.MyApplcation;
import bean.GoodsDetailBean;
import bean.GouWuCheBean;
import logincontroller.LoginController;
import presenter.OrderPresenter;
import presenter.ShopMallSearchPresenter;
import presenter.UserPresenter;
import util.BaseDialogFragment;
import util.CarouselView;
import util.ExpandTextView;
import util.utls.ByAlert;
import util.utls.Const;
import util.utls.IsUtilUid;
import util.utls.SPUtils;
import util.utls.XImageLoader;
import util.volley.DensityUtil;
import util.volley.ResultEntity;
import widget.FlowLayout;
import widget.XScrollView;


/**
 * 商品详情
 */
public class GoodDetailActivity extends BaseActivity implements Observer {
    ImageView back;
    TextView tvTitle;
    TextView goods_detail_title;//标题
    TextView goods_detail_price;//价格
    ExpandTextView goods_detail_jianjie;//简介
    ImageView more;
    TextView goods_guige;//规格
    TextView goods_detail_gouwuche, goods_detail_jiarugouwuche;
    LinearLayout li_choose_guige;//规格
    TextView goods_detail_price02;//原件
    CarouselView img;
    GoodsDetailBean detailBean = new GoodsDetailBean();
    ShopMallSearchPresenter presenter;
    UserPresenter userPresenter;
    RelativeLayout layout;
    LinearLayout shuxing;//属性
    String goodsId;
    String goodsName;
    PopupWindow mPopWin;
    TextView detail;

    int dian = 0;
    TextView tv_num;
    String type;//来标记是选择类型 还是加入购物车
    StringBuffer descid;
    StringBuffer moreCount;
    String uid;
    ImageView img_other;
    String tempSkuId = null;
    ImageView goods_tehui;
    String tehui;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_good_detail);
        goodsId = getIntent().getStringExtra("goodsId");
        presenter = new ShopMallSearchPresenter(this);
        userPresenter = new UserPresenter(this);
        showProgressDialog("数据加载中,请稍后...", true);
        presenter.mallXiangQing(goodsId);
        img_other.setBackgroundResource(R.drawable.gouwuche);
        img_other.setVisibility(View.VISIBLE);
        goodsName = getIntent().getStringExtra("goodsName");
        tehui = getIntent().getStringExtra("tehui");
        tvTitle.setText("商品详情");
        uid = (String) SPUtils.get(this, null, Const.ID, "");
        if (tehui != null) {
            if (tehui.equals("1")) {
                goods_tehui.setVisibility(View.VISIBLE);
                String uid = (String) SPUtils.get(MyApplcation.ctx, null, Const.JiaoBiao, "");
                XImageLoader.load(Const.IMAGE_HEAD + uid,
                        goods_tehui);
            } else {
                goods_tehui.setVisibility(View.GONE);
            }
        }
    }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    JiaRuGouWuCheDialog dialog;

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.goods_detail_jiarugouwuche:
                uid = (String) SPUtils.get(this, null, Const.ID, "");
                if (uid.equals("")) {
                    startActivity(new Intent(GoodDetailActivity.this, ActivityLogin.class));
                    return;
                }
                type = "jiaru";
                if (dialog == null)
                    dialog = new JiaRuGouWuCheDialog(detailBean, false);
                dialog.show(getSupportFragmentManager(), "");
                break;
            case R.id.img_other:
                LoginController.onGouWuChe(this,null);
                break;
            case R.id.goods_guige:
                uid = (String) SPUtils.get(this, null, Const.ID, "");
                if (uid.equals("")) {
                    startActivity(new Intent(GoodDetailActivity.this, ActivityLogin.class));
                    return;
                }
                type = "leixing";
                if (dialog == null)
                    dialog = new JiaRuGouWuCheDialog(detailBean, false);
                dialog.show(getSupportFragmentManager(), "");
                break;
            case R.id.more:
                boolean b = goods_detail_jianjie.getExpandableStatus();
                b = !b;
                if (b) {
                    more.setImageResource(R.drawable.less);
                } else {
                    more.setImageResource(R.drawable.more_unfold);
                }
                goods_detail_jianjie.setExpandable(b);
            default:
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);

        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if (entity.getEventType() == ShopMallSearchPresenter.mall_xq_success) {
                detailBean = (GoodsDetailBean) entity.getData();
                xiqngqing(detailBean);
            } else if (entity.getEventType() == ShopMallSearchPresenter.mall_xq_fail) {
                ByAlert.alert(entity.getData() + "");
            } else if (entity.getEventType() == UserPresenter.addAll_success) {
                ByAlert.alert(entity.getData() + "");
            } else if (entity.getEventType() == UserPresenter.addAll_fail) {
                ByAlert.alert(entity.getData() + "");
            }

        }
        try {
            closeProgressDialog();
        } catch (Exception e) {

        }
    }

    /**
     * 商品详情
     */
    private void xiqngqing(GoodsDetailBean detailBean) {
        goods_detail_title.setText(detailBean.getName());
        if (!detailBean.getSku_list().get(0).getPrice().equals("")) {
            goods_detail_price.setText("¥" + Double.parseDouble(detailBean.getSku_list().get(0).getPrice()) / 100);
        } else {
            goods_detail_price.setText("¥0");
        }
        if (!detailBean.getSku_list().get(0).getOri_price().equals("")) {
            goods_detail_price02.setText("¥" + Double.parseDouble(detailBean.getSku_list().get(0).getOri_price()) / 100);
            goods_detail_price02.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
        } else {
            goods_detail_price02.setText("¥0");
            goods_detail_price02.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
        }
        goods_detail_jianjie.setExpandable(false);
        goods_detail_jianjie.setText(detailBean.getSynopsis());
        if (detailBean.getSynopsis() != null) {
            //goods_detail_jianjie.getLines()完全伸展还需要的行数   goods_detail_jianjie.getLineCount()：当前实际行数
            if (goods_detail_jianjie.getLines() < goods_detail_jianjie.getLineCount()) {
                more.setVisibility(View.VISIBLE);
            } else {
                more.setVisibility(View.GONE);
            }
        }
        //轮播
        if (detailBean != null) {
            List<String> imgIds = detailBean.getCarousel_images();
            imgIds.add(0, detailBean.getMain_img());
            ArrayList<ImageView> arrayBmps = new ArrayList<ImageView>();
            for (int i = 0; i < imgIds.size(); i++) {
                ImageView imageView = new ImageView(GoodDetailActivity.this);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                String m = imgIds.get(i);
                XImageLoader.load(Const.IMAGE_HEAD + m, imageView);
                arrayBmps.add(imageView);
                imageView.setTag(i);
            }
            img.setImageBitmaps(arrayBmps);
            // //让顶部控件获取到焦点这样滚动条就自然停在顶部了
            img.setFocusable(true);
            img.setFocusableInTouchMode(true);
            img.requestFocus();
        }
        //属性
        if (detailBean.getProperties() != null) {
            if (detailBean.getProperties().size() > 0) {
                for (int i = 0; i < detailBean.getProperties().size(); i++) {
                    View viewStore = LayoutInflater.from(getApplicationContext())
                            .inflate(R.layout.item_shuxing, null);
                    TextView txt_goodname = (TextView) viewStore.findViewById(R.id.txt_goodname);

                    StringBuffer sb = new StringBuffer();

                    if (detailBean.getProperties().get(i).getProp_value_list().size() > 0) {
                        for (int j = 0; j < detailBean.getProperties().get(i).getProp_value_list().size(); j++) {

                            sb.append(detailBean.getProperties().get(i).getProp_value_list().get(j).getValue_name() + ";");
                        }
                    }
                    String value_name = sb.substring(0, sb.length() - 1);

                    txt_goodname.setText(detailBean.getProperties().get(i).getProp_name() + "  :  " + value_name);
                    LinearLayout.LayoutParams layoutparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    layoutparams.topMargin = 1;
                    shuxing.addView(viewStore, layoutparams);

                }
            }
        }

    }

    @SuppressLint("ValidFragment")
    public class JiaRuGouWuCheDialog extends BaseDialogFragment implements Observer{
        View close;
        ImageView pic;

        ViewGroup guigeContainer;
        View ok;
        GoodsDetailBean goodsDetailBeans;
        View jian;
        View jia;
        EditText input;
        boolean isLiJIGouMai;
        ViewGroup container;
        String imgUrl;
        protected String price;
        private FlowLayout flowLayout;
        TextView txt_price;
        TextView txt_kc;
        int kucun;//标记库存

        OrderPresenter presenter;
        private List<GouWuCheBean> beanList;
        private String gouwunum="";//购物车的数量

        public JiaRuGouWuCheDialog() {
        }

        public JiaRuGouWuCheDialog(GoodsDetailBean goodsDetailBeans,
                                   boolean isLiJIGouMai) {
            this.goodsDetailBeans = goodsDetailBeans;
            this.isLiJIGouMai = isLiJIGouMai;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            return LayoutInflater.from(getContext()).inflate(
                    R.layout.dialog_add_shopcart, null);
        }


        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            presenter = new OrderPresenter(this);
            presenter.gouWuChe(uid);

            imgUrl = goodsDetailBeans.getMain_img();
//            XImageLoader.load(Const.IMAGE_URL + "/picture/index?id="+ goodsDetailBeans.getMain_img(), pic);

            XImageLoader.load(Const.IMAGE_HEAD + goodsDetailBeans.getMain_img(), pic);
            //  txt_price.setText(Double.parseDouble(goodsDetailBeans.getSku_list().get(0).getPrice()) / 100 + "元/" + goodsDetailBeans.getGoods_unit_name());
            //   txt_kc.setText(goodsDetailBeans.getSku_list().get(0).getQuantity()+ goodsDetailBeans.getGoods_unit_name());

            //显示价格（点进去的时候显示最低价格和最高价格的区间）  当只有一个规格时显示一个价格
            double da = Double.parseDouble(goodsDetailBeans.getSku_list().get(0).getPrice());
            double xiao = Double.parseDouble(goodsDetailBeans.getSku_list().get(0).getPrice());
            if (goodsDetailBeans.getSku_list().size() > 1) {
                for (int i = 0; i < goodsDetailBeans.getSku_list().size(); i++) {
                    double d = Double.parseDouble(goodsDetailBeans.getSku_list().get(i).getPrice());
                    if (da < d) {
                        da = d;
                    }
                    if (xiao > d) {
                        xiao = d;
                    }
                }
                txt_price.setText(String.valueOf(xiao / 100) + "-" + String.valueOf(da / 100) + "元/" + goodsDetailBeans.getGoods_unit_name());
            } else {
                txt_price.setText(String.valueOf(xiao / 100) + "元/" + goodsDetailBeans.getGoods_unit_name());
            }
            //显示库存(首次显示所有数量的和)
            int shuliang = 0;
            for (int i = 0; i < goodsDetailBeans.getSku_list().size(); i++) {
                int d = Integer.parseInt(goodsDetailBeans.getSku_list().get(i).getQuantity());
                shuliang += d;
            }
            kucun = shuliang;
            txt_kc.setText(String.valueOf(kucun) + goodsDetailBeans.getGoods_unit_name());

            if (goodsDetailBeans.getSku_info().size() <= 0) {//没有规格

            } else {//有规格

            }
            addGuiGe();
            pic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), ImageShowerActivity.class);
                    intent.putExtra("imgUrl", imgUrl);
                    startActivity(intent);
                }
            });
            jia.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String sb = checkGuiGeSelectedState();
                    if (sb == null) {
                        return;
                    }
                    String s = input.getText().toString();
                    int ss=0;
                    if(!s.equals("")){
                        ss = Integer.parseInt(s);
                    }
                    if (ss >= kucun) {
                        ByAlert.alert("库存不足,您的购物车已有"+(gouwunum.equals("")?"0":gouwunum)+goodsDetailBeans.getGoods_unit_name());
                        return;
                    }
                    try {
                        int c = Integer.parseInt(s);
                        input.setText(c + 1 + "");
                    } catch (NumberFormatException e) {
                        input.setText("1");
                    }
                    input.setSelection(input.getText().length());
                }
            });
            jian.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String sb = checkGuiGeSelectedState();
                    if (sb == null) {
                        return;
                    }
                    String s = input.getText().toString();
                    try {
                        int c = Integer.parseInt(s);
                        if (c > 1) {
                            c--;
                        }
                        input.setText(c + "");
                    } catch (NumberFormatException e) {
                        input.setText("1");
                    }
                    input.setSelection(input.getText().length());
                }
            });
            close.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    List<String> xuanzhong = selectedGuige();
                    StringBuffer pkidTemp = new StringBuffer();
                    StringBuffer stringBuffer = new StringBuffer();
                    if (xuanzhong != null) {
                        for (String s1 : xuanzhong) {
                            stringBuffer.append(s1 + ";");
                            pkidTemp.append(s1 + ";");
                            tempSkuId = pkidTemp.toString();
                        }
                        if (xuanzhong.size() <= 0) {
                            tempSkuId = null;
                        }
                    } else {
                        tempSkuId = null;
                    }
                    for (int i = 0; i < goodsDetailBeans.getSku_list().size(); i++) {
                        String sid = goodsDetailBeans.getSku_list().get(i).getSku_id();

                        if (sid.equals(stringBuffer.toString())) {
                            goods_guige.setText(goodsDetailBeans.getSku_list().get(i).getSku_desc());
                        }
                    }
                    closeDialog();
                }
            });
            ok.setOnClickListener(new View.OnClickListener() {
                public String pskuId;

                @Override
                public void onClick(View v) {

                    String uid = SPUtils.get(GoodDetailActivity.this, null, Const.ID, "") + "";
                    if (uid.equals("")) {
                        startActivity(new Intent(GoodDetailActivity.this, ActivityLogin.class));
                        return;
                    }
                    String s = checkGuiGeSelectedState();
                    if (s == null) {
                        return;
                    }
                    List<GoodsDetailBean.SkuList> list = goodsDetailBeans
                            .getSku_list();
                    for (GoodsDetailBean.SkuList skuCombinationBean : list) {
                        if (skuCombinationBean.getSku_id().equals(s)) {
                            pskuId = skuCombinationBean.getSku_id();
                            break;
                        }
                    }
                    ok.setEnabled(false);
                    if (input.getText().toString().equals("") || input.getText().toString().equals("0")) {
                        ByAlert.alert("请输入有效数量");
                        return;
                    }
                    showProgressDialog("正在添加中...", false);
                    if (goodsDetailBeans.getSku_info().size() > 0) {//多规格
                        String guigeID = "";//规格id

                        List<String> xuanzhong = selectedGuige();
                        StringBuffer stringBuffer = new StringBuffer();
                        if (xuanzhong != null) {
                            for (String s1 : xuanzhong) {
                                stringBuffer.append(s1 + ";");
                            }
                        }
                        for (int i = 0; i < goodsDetailBeans.getSku_list().size(); i++) {
                            String sid = goodsDetailBeans.getSku_list().get(i).getSku_id();
                            String pkid = goodsDetailBeans.getSku_list().get(i).getSku_pkid();
                            if (sid.equals(stringBuffer.toString())) {
                                guigeID = pkid;
                            }
                        }
                        userPresenter.addMallAll(uid, goodsDetailBeans.getId(), guigeID, input.getText().toString());

                    } else {//单规格
                        userPresenter.addMallAll(uid, goodsDetailBeans.getId(), goodsDetailBeans.getSku_pkid(), input.getText().toString());
                    }
                    dismiss();
                }
            });
            input.addTextChangedListener(watcher);
        }
        /**
         *  输入数量时候的监听
         */
        TextWatcher watcher = new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //只要编辑框内容有变化就会调用该方法，s为编辑框变化后的内容
                Log.i("onTextChanged", s.toString());

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                //编辑框内容变化之前会调用该方法，s为编辑框内容变化之前的内容
                Log.i("beforeTextChanged", s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {
                //编辑框内容变化之后会调用该方法，s为编辑框内容变化后的内容
                Log.i("afterTextChanged", s.toString());
                if(!s.toString().equals("")){
                    int shuru=Integer.parseInt(s.toString());
                    if(shuru>kucun){
                        input.setText("1");
                    }
                }
            }
        };
        public ResultEntity handlerError(Object data) {
            ResultEntity result = (ResultEntity) data;
            if (result == null) {
                result = new ResultEntity("-1", "数据格式错误!~", data);
                return result;
            }

            if (result.hasError()) {
                ByAlert.alert(result.getMsg());
                result.setEventType(EVENT_TYPE_UNKNOWN);
                return result;
            }
            return result;
        }

        protected String checkGuiGeSelectedState() {

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < guigeContainer.getChildCount(); i++) {
                View vv = guigeContainer.getChildAt(i);
                if (!(vv instanceof FlowLayout)) {
                    continue;
                }
                FlowLayout flowLayout = (FlowLayout) vv;
                int j;
                for (j = 0; j < flowLayout.getChildCount(); j++) {
                    TextView textView = (TextView) flowLayout.getChildAt(j);
                    if (textView.isSelected()) {
                        sb.append(textView.getTag() + ";");
                        break;
                    }
                }
                if (j == flowLayout.getChildCount()) {
                    ByAlert.alert("请选择商品");
                    return null;
                }
            }
            return sb.toString();
        }


        //规格
        private void addGuiGe() {

            List<GoodsDetailBean.SkuInfo> list = goodsDetailBeans
                    .getSku_info();
            for (int i = 0; i < list.size(); i++) {
                GoodsDetailBean.SkuInfo skuBean = list.get(i);
                TextView title = createTitle(skuBean.getSku_name());
                guigeContainer.addView(title);

                flowLayout = createFlowLayout(skuBean.getSku_id(),
                        goodsDetailBeans.getSku_info().get(i).getValue_list(),
                        "");
                guigeContainer.addView(flowLayout);
            }
            container.getViewTreeObserver().addOnGlobalLayoutListener(
                    new ViewTreeObserver.OnGlobalLayoutListener() {

                        @SuppressWarnings("deprecation")
                        @SuppressLint("NewApi")
                        @Override
                        public void onGlobalLayout() {
                            try {
                                container.getViewTreeObserver()
                                        .removeGlobalOnLayoutListener(this);
                            } catch (Throwable e) {
                            }
                            if (container.getHeight() > DensityUtil
                                    .screenHeigh() * 0.7) {

                                int h = DensityUtil.screenHeigh()
                                        - DensityUtil.dip2px(280);
                                XScrollView scrollView = new XScrollView(
                                        getContext());
                                scrollView.setLayoutParams(new ViewGroup.LayoutParams(
                                        ViewGroup.LayoutParams.MATCH_PARENT, h));

                                ViewGroup vg = (ViewGroup) guigeContainer
                                        .getParent();
                                vg.removeView(guigeContainer);

                                scrollView.addView(guigeContainer);
                                vg.addView(scrollView);
                            }
                        }
                    });
        }

        private View.OnClickListener clickListener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (v.isSelected()) {
                    v.setSelected(false);
                    ((TextView) v).setTextColor(Color.BLACK);
                    imgUrl = goodsDetailBeans.getMain_img();
                    if ((!goodsDetailBeans.getMain_img().equals(""))
                            & goodsDetailBeans.getMain_img() != null) {
//                        XImageLoader.load(Const.imgurl + goodsDetailBeans.getMain_img(),pic);
                        XImageLoader.load(Const.IMAGE_HEAD + goodsDetailBeans.getMain_img(), pic);
                    }
                    // txt_price.setText(Double.parseDouble(goodsDetailBeans.getSku_list().get(0).getPrice()) / 100 + "元/" + goodsDetailBeans.getGoods_unit_name());
                    //   txt_kc.setText(goodsDetailBeans.getSku_list().get(0).getQuantity()+ goodsDetailBeans.getGoods_unit_name());
                    //显示价格（未选中时显示最低价格和最高价格的区间）  当只有一个规格时显示一个价格
                    double da = Double.parseDouble(goodsDetailBeans.getSku_list().get(0).getPrice());
                    double xiao = Double.parseDouble(goodsDetailBeans.getSku_list().get(0).getPrice());
                    if (goodsDetailBeans.getSku_list().size() > 1) {
                        for (int i = 0; i < goodsDetailBeans.getSku_list().size(); i++) {
                            double d = Double.parseDouble(goodsDetailBeans.getSku_list().get(i).getPrice());
                            if (da < d) {
                                da = d;
                            }
                            if (xiao > d) {
                                xiao = d;
                            }
                        }
                        txt_price.setText(String.valueOf(xiao / 100) + "-" + String.valueOf(da / 100) + "元/" + goodsDetailBeans.getGoods_unit_name());
                    } else {
                        txt_price.setText(String.valueOf(xiao / 100) + "元/" + goodsDetailBeans.getGoods_unit_name());
                    }

                    //显示库存(未选中显示所有数量的和)
                    int shuliang = 0;
                    for (int i = 0; i < goodsDetailBeans.getSku_list().size(); i++) {
                        int d = Integer.parseInt(goodsDetailBeans.getSku_list().get(i).getQuantity());
                        shuliang += d;
                    }
                    kucun = shuliang;
                    txt_kc.setText(String.valueOf(kucun) + goodsDetailBeans.getGoods_unit_name());

                    return;
                }
                FlowLayout clickTextViewParent = null;
                StringBuilder stringBuilder = new StringBuilder();

                for (int i = 0; i < guigeContainer.getChildCount(); i++) {
                    View view = guigeContainer.getChildAt(i);
                    if (!(view instanceof FlowLayout)) {
                        continue;
                    }
                    FlowLayout flowLayout = (FlowLayout) view;
                    if (v.getParent() == flowLayout) {
                        clickTextViewParent = flowLayout;
                        stringBuilder.append(v.getTag());
                        stringBuilder.append(";");

                    } else {
                        for (int j = 0; j < flowLayout.getChildCount(); j++) {
                            View textview = flowLayout.getChildAt(j);
                            if (textview.isSelected()) {
                                stringBuilder.append(textview.getTag());
                                stringBuilder.append(";");
                                break;
                            }
                        }
                    }
                }
                List<GoodsDetailBean.SkuList> skuList = goodsDetailBeans.getSku_list();
                String selectedSku = stringBuilder.toString();
                for (int i = 0; i < skuList.size(); i++) {
                    String sku = skuList.get(i).getSku_id();
                    String kc = skuList.get(i).getQuantity();
                    for (int j = 0; j < clickTextViewParent.getChildCount(); j++) {
                        if (clickTextViewParent.getChildAt(j).isSelected()) {
                            clickTextViewParent.getChildAt(j).setSelected(false);
                            ((TextView) clickTextViewParent.getChildAt(j)).setTextColor(Color.BLACK);
                            break;
                        }
                    }
                    v.setSelected(true);
                    // txt_price.setText(Double.parseDouble(v.getTag(R.id.tag_first) + "") / 100 + "元/" + goodsDetailBeans.getGoods_unit_name());//（选择两个的时候显示的是当前点击的标签价格）
                    ((TextView) v).setTextColor(Color.WHITE);
                    if (sku.equals(selectedSku)) {
                        if (kc.equals("0")) {
                            ByAlert.alert("该商品没有库存了");
                            ok.setEnabled(false);//没有库存不可点击
                        } else {
                            ok.setEnabled(true);
                        }
                        txt_price.setText(Double.parseDouble(skuList.get(i).getPrice()) / 100 + "元/" + goodsDetailBeans.getGoods_unit_name());
                        kucun = Integer.parseInt(goodsDetailBeans.getSku_list().get(i).getQuantity());
                        txt_kc.setText(kucun + goodsDetailBeans.getGoods_unit_name());
                    }

                }
                String s = isAllFenLeiSelected();
                if (s != null) {
//                        ((View) kucun.getParent()).setVisibility(View.VISIBLE);
                    List<GoodsDetailBean.SkuList> list = goodsDetailBeans
                            .getSku_list();
                    for (GoodsDetailBean.SkuList skuCombinationBean : list) {
                        if (skuCombinationBean.getSku_id().equals(s)) {
//                                kucun.setText(skuCombinationBean.getQuantity()
//                                        + "");
                            price = skuCombinationBean.getPrice();

                            if (TextUtils.isEmpty(skuCombinationBean
                                    .getIcon_url())) {
                                XImageLoader.load(Const.IMAGE_HEAD + goodsDetailBeans.getMain_img(), pic);
                                imgUrl = goodsDetailBeans.getMain_img();
                            } else {
                                XImageLoader.load(Const.IMAGE_HEAD + skuCombinationBean.getIcon_url(), pic);
                                imgUrl = skuCombinationBean.getIcon_url();
                            }
                            break;
                        }
                    }
                }
            }
        };

        private String isAllFenLeiSelected() {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < guigeContainer.getChildCount(); i++) {
                View vv = guigeContainer.getChildAt(i);
                if (!(vv instanceof FlowLayout)) {
                    continue;
                }
                FlowLayout flowLayout = (FlowLayout) vv;
                int j;
                for (j = 0; j < flowLayout.getChildCount(); j++) {
                    TextView textView = (TextView) flowLayout.getChildAt(j);
                    if (textView.isSelected()) {
                        sb.append(textView.getTag() + ";");
                        break;
                    }
                }
                if (j == flowLayout.getChildCount()) {
                    return null;
                }
            }
            return sb.toString();
        }

        /**
         * 当前已选中的规格集合
         *
         * @return
         */
        protected List<String> selectedGuige() {

            List<String> selectedGuige = new ArrayList<String>();
            for (int i = 0; i < guigeContainer.getChildCount(); i++) {
                View vv = guigeContainer.getChildAt(i);
                if (!(vv instanceof FlowLayout)) {
                    continue;
                }
                FlowLayout flowLayout = (FlowLayout) vv;
                for (int j = 0; j < flowLayout.getChildCount(); j++) {
                    TextView textView = (TextView) flowLayout.getChildAt(j);
                    if (textView.isSelected()) {
                        selectedGuige.add((String) textView.getTag());
                    }
                }
            }
            return selectedGuige;
        }

        private FlowLayout createFlowLayout(String vid, List<GoodsDetailBean.ValueList> id,
                                            String str) {
            int dis = DensityUtil.dip2px(5);
            FlowLayout flowLayout = new FlowLayout(getContext());
            for (int i = 0; i < id.size(); i++) {
                TextView textView = new TextView(getContext());
                textView.setTextColor(Color.BLACK);
                textView.setBackgroundResource(R.drawable.selector_guige);
                textView.setText(id.get(i).getValue_name());
                textView.setTag(vid + ":" + id.get(i).getValue_id());
                textView.setTag(R.id.tag_first, goodsDetailBeans.getSku_list().get(i).getPrice());
                textView.setOnClickListener(clickListener);
                if (tempSkuId != null) {
                    String[] strTemp = tempSkuId.split(";");
                    for (String s : strTemp) {
                        if (s.equals(textView.getTag().toString())) {
                            textView.setSelected(true);
                        }
                    }

                }
                ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                marginLayoutParams.setMargins(dis, dis, dis, dis);
                textView.setLayoutParams(marginLayoutParams);
                flowLayout.addView(textView);
            }
            return flowLayout;
        }

        /**
         * 设置套餐一、套餐二等标题栏
         *
         * @param s
         * @return
         */
        private TextView createTitle(String s) {
            TextView textView = new TextView(getContext());
            textView.setText(s);
            textView.setTextColor(Color.BLACK);
            return textView;
        }

        public void closeDialog() {
            View v = getView().findViewById(R.id.container);
            ObjectAnimator animator = ObjectAnimator.ofFloat(v, "translationY",
                    0, v.getHeight()).setDuration(300);
            animator.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator arg0) {
                }

                @Override
                public void onAnimationRepeat(Animator arg0) {
                }

                @Override
                public void onAnimationEnd(Animator arg0) {
                    dismiss();
                }

                @Override
                public void onAnimationCancel(Animator arg0) {
                    dismiss();
                }
            });
            animator.start();
        }

        @Override
        public void update(Observable observable, Object data) {
            ResultEntity entity = handlerError(data);
            if (entity != null) {
                IsUtilUid.tianjia(entity);
                if (entity.getEventType() == OrderPresenter.addAll_success) {
                    beanList = (List<GouWuCheBean>) entity.getData();
                    for (int i=0;i<beanList.size();i++){
                        if(goodsDetailBeans.getSku_pkid().equals(beanList.get(i).getSku_pid())){
                            gouwunum=  beanList.get(i).getCount()+"";
                        }
                    }
                }
            }
        }
    }

}